var map = new BMap.Map("allmap");
var len = "";
var arrys = [];
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r) {
	var addComp = r.address;
	var citys = addComp.city;
	var lat = r.point.lat;
	var lng = r.point.lng;
	if(this.getStatus() == BMAP_STATUS_SUCCESS) {
		var mk = new BMap.Marker(r.point);
		var point = new BMap.Point(113.837, 22.745);
		map.centerAndZoom(point, 15);
		var myIcon = new BMap.Icon("../public/cache/1.png", new BMap.Size(350, 80));
		var mk = new BMap.Marker(point, {
			icon: myIcon
		});
		map.panTo(r.point);
		//alert('您的位置：' + r.point.lng + ',' + r.point.lat);

		var lables = new BMap.Label("<div class='box'>10<h5>分钟</h5><span class='d'>在这里上车</span></div>", {
			offset: new BMap.Size(-66, -95)

		});
		mk.setLabel(lables);
		map.addOverlay(mk);

		mk.enableDragging();

		mk.addEventListener("dragend", function(e) { //点击标注时出发事件
			//map.clearOverlays();
			document.getElementById("mapx").value = e.point.lng;
			document.getElementById("mapy").value = e.point.lat;

			var lat = e.point.lat;
			var lng = e.point.lng;
			var pt = e.point;
			var geoc = new BMap.Geocoder();
			geoc.getLocation(pt, function(rs) {
				var addComp = rs.addressComponents;
				var citys = addComp.city;
				var sdp = rs.surroundingPois.length;
				if(sdp !== 0) {
					$('#initial_position').val(rs.surroundingPois[0].title);
				} else {
					$('#initial_position').val(addComp.street + addComp.streetNumber);
				}
				document.addEventListener("plusready", onPlusReady, false);

				function onPlusReady() {
					var acc = plus.storage.getItem('account');
					var tokens = plus.storage.getItem('token');

					//map.removeOverlay(marker2);
					
					for(var z = 0; z < arrys.length; z++) {
						deletePoint();
					}
					function deletePoint() { //删除标注点
						//getMapLabel();
						var allOverlay = map.getOverlays();
						

							for(var i = 0; i < allOverlay.length - 1; i++) {
								for(var j = 0; j < arrys.length; j++) {
									arrvalue = arrys[j]; //数组的索引是从0开始的
							//把取出的值打印在控制台上
								console.log(allOverlay[i].toString())

								if(allOverlay[i].toString() == "[object Marker]") {
									var lables = allOverlay[i].getLabel().content;
									var ids = $(lables).attr("id");

									if(ids == arrvalue) {
										map.removeOverlay(allOverlay[i]);

										return false;
									}
								}
							}

						}
					};

					getDriver(acc, tokens, citys, lat, lng);

				}
				$.ajaxSetup({
					cache: false
				});
				onPlusReady();

			});
		});

	} else {
		alert('failed' + this.getStatus());
	}

}, {
	enableHighAccuracy: true
});

function getDriver(acc, tokens, citys, lat, lng) {
	$.ajax({
		type: "post",
		url: webRoot + "?s=/App/Username/getNearbyDriver",
		data: {
			account: acc,
			token: tokens,
			city: citys,
			lat: lat,
			lng: lng
		},

		async: false,
		success: function(d) {
			var data = $.parseJSON(d);
			len = data.data.length;
			console.log(JSON.stringify(data.data));

			if(len > 0) {
				//map.clearOverlays();
				arrys = [];
				for(var i = 0; i < len; i++) {
					var point = new BMap.Point(data.data[i].dlng, data.data[i].dlat);
					var ph = JSON.stringify(data.data[i].photo);
					arrys.push(data.data[i].id);

					//var marker2 = new BMap.Marker(point);  // 创建标注
					var myIcon = new BMap.Icon("../public/cache/7.png", new BMap.Size(5, 5));
					marker2 = new BMap.Marker(point, {
						icon: myIcon
					}); // 创建标注
					map.addOverlay(marker2);

					map.addOverlay(marker2);
					var myLabel = new BMap.Label("<div class='lables' id=" + data.data[i].id + "><img src=" + ph + "/><div class='appraise-star' onclick='Cars()'>" + data.data[i].username + "<div class='store-star'><div class='star-outer'><div class='star-inner' data-level=" + data.data[i].score + "></div></div></div></div></div>", {
						offset: new BMap.Size(0, 0)
					});

					marker2.setLabel(myLabel);

					marker2.addEventListener("click", function(e) {
						var me = e.target.getLabel().content;
						var ids = $(me).attr("id");
						tool.openNewPage('driver_info.html', 'driver_info.html', {
							did: ids
						}).show('fade-in');
					});

					tool.calculateStarWidth(); //计算等级星星的宽度

				}
				alert(arrys);
			}
		}

	});
}